<template>
  <div class="employees">
    <!-- 工具栏组件 -->
    <mytools>
      <template #right>
        <el-button size="mini" type="primary" @click="toimport">导入</el-button>
        <el-button size="mini" type="primary" @click="showadd = true">
          <i class="el-icon-plus"></i> 新增员工</el-button
        >
      </template>
    </mytools>
    <ployeespanel
      @updateData="getUserList"
      :showadd.sync="showadd"
    ></ployeespanel>
    <!-- 表格数据 -->
    <el-card class="mycard">
      <el-table :data="list" border>
        <el-table-column
          label="序号"
          type="index"
          fixed="left"
        ></el-table-column>
        <el-table-column label="姓名" prop="username"></el-table-column>
        <el-table-column label="头像" align="center" width="150px">
          <template slot-scope="{ row }" class="imgBox">
            <img
              @click="showImg(row.staffPhoto)"
              v-errimg="deImg"
              :src="row.staffPhoto"
              alt=""
              width="50px"
              height="50px"
            />
          </template>
        </el-table-column>
        <el-table-column label="手机号" prop="mobile"></el-table-column>
        <el-table-column label="工号" prop="workNumber"></el-table-column>
        <el-table-column label="聘用形势" prop="formOfEmployment">
          <template slot-scope="{ row }">
            {{ row.formOfEmployment | myEmployees }}
          </template>
        </el-table-column>
        <el-table-column label="部门" prop="departmentName"></el-table-column>
        <el-table-column label="入职时间">
          <template slot-scope="{ row }">
            {{ row.timeOfEntry | timeformata }}
          </template>
        </el-table-column>
        <el-table-column label="是否在职">
          <template> 在职 </template>
        </el-table-column>
        <el-table-column label="状态">
          <template> 可用 </template>
        </el-table-column>
        <el-table-column label="操作" width="260" fixed="right">
          <template slot-scope="{ row }">
            <el-button
              :disabled="'employees-check' | hasRigo"
              type="text"
              @click="lookUser(row)"
              >查看</el-button
            >
            <el-button type="text">转正</el-button>
            <el-button type="text">调岗</el-button>
            <el-button type="text">离职</el-button>
            <el-button type="text" @click="lookRoles(row)">角色</el-button>
            <el-button :disabled="'employees-del' | hasRigo" type="text"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 二维码的弹出层 -->
      <el-dialog title="扫码预览图片" :visible.sync="ewmshow">
        <canvas ref="myCanvas" />
      </el-dialog>
      <!-- 分配角色对话框 -->
      <el-dialog title="分配角色" :visible.sync="roleShow">
        <el-form>
          <el-form-item>
            <el-checkbox-group v-model="checklist">
              <el-checkbox
                v-for="(item, index) in roleList"
                :key="index"
                :label="item.id"
              >
                {{ item.name }}
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item class="myitem">
            <el-button @click="canel">取 消</el-button>
            <el-button @click="submit" type="primary">确 定</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { getEmployeesAllListApi, setRoleApi } from "@/api/employees";
import { getUserDetailById } from "@/api/user";
import { getRoleListApi } from "@/api/setting";
import QrCode from "qrcode";
export default {
  data() {
    return {
      deImg: require("@/assets/common/head.jpg"),
      list: [],
      currentPage: 1,
      pageSizes: [8, 16, 24, 32],
      pageSize: 8,
      total: 0,
      showadd: false,
      //二维码对话框******
      ewmshow: false,
      //角色对话框
      roleShow: false,
      //复选框中被选中的列表
      checklist: [],
      //角色列表
      roleList: [],
      //当前数据对象
      currentObj: {},
    };
  },
  methods: {
    async submit() {
      await setRoleApi({
        id: this.currentObj.id,
        roleIds: this.checklist,
      });
      this.$message.success("角色分配成功");
      this.roleShow = false;
      //更新数据
      this.getRoleList();
    },
    canel() {
      this.roleShow = false;
    },
    async lookRoles(row) {
      this.currentObj = row;
      this.roleShow = true;
      const res = await getUserDetailById(row.id);
      this.checklist = res.data.roleIds;
    },
    async getRoleList() {
      const res = await getRoleListApi();
      this.roleList = res.data.rows;
    },
    showImg(img) {
      this.ewmshow = true;
      this.$nextTick(() => {
        // 将图片转为二维码，渲染到页面上
        QrCode.toCanvas(this.$refs.myCanvas, img, {
          width: 200,
          height: 200,
        });
      });
    },
    lookUser(row) {
      this.$router.push(`/employees/details/${row.id}`);
    },
    toimport() {
      this.$router.push("/employees/import");
    },
    //获取员工列表
    async getUserList() {
      const res = await getEmployeesAllListApi({
        page: this.currentPage,
        size: this.pageSize,
      });
      this.list = res.data.rows;
      this.total = res.data.total;
    },
    // 页容量改变时触发
    handleSizeChange(size) {
      this.pageSizes = size;
      this.getUserList();
    },
    // 当前页改变时触发
    handleCurrentChange(page) {
      this.currentPage = page;
      this.getUserList();
    },
  },
  created() {
    this.getUserList();
    this.getRoleList();
  },
  components: {
    ployeespanel: () => import("./components/ployeespanel.vue"),
  },
};
</script>

<style lang="scss">
.employees {
  margin: 20px;
  .imgBox {
    width: 100px;
  }
  .mycard {
    margin-top: 20px;
  }
}
.el-table__header {
  th:last-child {
    .cell {
      text-align: center;
    }
  }
}
</style>
